﻿/**
*MenuInfo class
*auth:lww
*version:1.0
*date:2014-10-11
*/
function MenuInfo(name, context, flag) {
    this.id = "";    
    this.name = "MenuInfo";
    this.version = "1.0.0";
    this.mContext = context;
    this.type = null;  
    /**
    *MenuInfo
    */      
    this.mId = null;
    this.mName = name;    
    this.mPath = null;
    this.mPars = null;
    this.mIco = null;
    this.mCode = null;   
    this.sortIndex = 0;
    this.isLeaf = 0;
    this.rootFlag = flag;
    /**
    *Menu Item List
    */
    this.mItemList = null;
    this.mIndex = 0;
}
MenuInfo.prototype.getId = function () {
    return this.mId;
};
MenuInfo.prototype.getName = function () {
    return this.mName;
};
MenuInfo.prototype.getCode = function () {
    return this.mCode;
};
MenuInfo.prototype.isRoot = function () {
    return this.rootFlag;
};
MenuInfo.prototype.getPath = function () {
    return this.mPath;
};
MenuInfo.prototype.setPath = function (url) {
    this.mPath = url;
};
MenuInfo.prototype.getPars = function () {
    return this.mPars;
};
MenuInfo.prototype.getIco = function () {
    return this.mIco;
};
MenuInfo.prototype.addItem = function (m) {
    if (isNull(m))
        return;
    if (this.mItemList == null) {
        this.mItemList = new Array();
        this.mIndex = 0;
    }
    this.mItemList[this.mIndex++] = m;
};
MenuInfo.prototype.getItem = function (index) {
    if (this.mItemList == null) {
        return null;
    }
    else if (index >= 0 && index < this.mItemList.length) {
        return this.mItemList[index];
    }
};
MenuInfo.prototype.getItemCount = function () {
    if (this.mItemList == null) {
        return 0;
    }
    else {
        return this.mItemList.length;
    }
};
MenuInfo.prototype.itemEach = function (callfun) {
    if (this.mItemList == null) {
        return;
    }
    for(var item of this.mItemList){
        callfun(item);
    }   
};
/**
 * parseMenuInfo
 */
function parseMenuInfo(menuObj, dataObj) {
    if (isNull(dataObj) || isNull(dataObj.menuList)) {
        return;
    }
    for(var dataItem of dataObj.menuList){        
        var menuItem = new MenuInfo("menu", menuObj, false);   
        menuItem.id = dataItem.id;
        menuItem.mId = dataItem.menuId;       
        menuItem.mName = dataItem.menuName   
        menuItem.mCode = dataItem.menuCode;        
        menuItem.mPath = dataItem.menuPath;
        menuItem.mPars = dataItem.menuParams;
        menuItem.mIco = dataItem.menuIco;
        menuItem.sortindex = dataItem.sortindex;
        menuItem.isLeaf = dataItem.isLeaf;        
        //parse childItemList
        parseMenuInfo(menuItem, dataItem);
        //add to parentItemList
        menuObj.addItem(menuItem);
    }    
}

function getMenuInfo(resObj,context){    
    var menuInfo = new MenuInfo("root",context, true);
    if(isNull(resObj)){
        return menuInfo;
    }
    parseMenuInfo(menuInfo,resObj.data);
    return menuInfo;
}

function updateMenuPath(menuInfo,menuName,path){
    menuInfo.itemEach(function(item){    
        if(item.getName() === menuName)   {
            item.setPath(path);
        }
        else{
            updateMenuPath(item,menuName,path);
        }
    });
}

markscript("MenuInfo");
//end MenuInfo.js